#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import shutil
from filecmp import dircmp

#### --------------------------------------------------
# 注：nodediffpatch 项目要放到和reactnative 同级目录下
# 运行命令 npm run patch-all
# --------------------------------------------------

### ********** 补丁对应的版本号 **********
version_last = raw_input("输入素材补丁支持的最低版本号（格式：1.0.0）:")
version_new = raw_input("输入补丁支持的最高版本号（格式：1.0.0）:")


### ********** 可变配置项 **********
#压缩包中的全部文件的目标文件夹 相对路径
target_relative_dir_ios = 'bundle/ios-HMDS-patch-'+version_last+'-'+version_new
#target_relative_dir_android = 'bundle/android-HMDS-patch-'+version_last+'-'+version_new


# ********** 生成js补丁文件 并拷贝到统一目录 *********
#RN项目根目录
rn_project_root = os.getcwd()

os.chdir('../../nodediffpatch/')

# 拷贝高低版本的jsbundle包到path目录
temp_path_bundle_last = rn_project_root+'/bundle/ios/ios-HMDS-'+version_last+'/index.ios.bundle'
temp_path_bundle_new = rn_project_root+'/bundle/ios/ios-HMDS-'+version_new+'/index.ios.bundle'

if os.path.exists(os.getcwd()+'/patch/index.ios.bundle'): os.remove(os.getcwd()+'/patch/index.ios.bundle')
if os.path.exists(os.getcwd()+'/patch/index.ios_new.bundle'): os.remove(os.getcwd()+'/patch/index.ios_new.bundle')
if os.path.exists(os.getcwd()+'/patch/patches.pat'): os.remove(os.getcwd()+'/patch/patches.pat')

shutil.copyfile(temp_path_bundle_new, os.getcwd()+'/patch/index.ios_new.bundle')
shutil.copyfile(temp_path_bundle_last, os.getcwd()+'/patch/index.ios.bundle')

# diff生成补丁
os.system('patbundle patch -o index.ios.bundle -n index.ios_new.bundle')

#补丁文件路径
patch_path = os.getcwd() + '/patch/patches.pat'


#压缩包中的全部文件的目标文件夹 绝对路径
target_dir_ios = rn_project_root+'/'+target_relative_dir_ios
#target_dir_android = rn_project_root+'/'+target_relative_dir_android

if not os.path.exists(target_dir_ios):
    os.makedirs(target_dir_ios)

# 拷贝pat文件
shutil.copyfile(patch_path, target_dir_ios+'/patches.pat')
# 拷贝meta文件
shutil.copyfile(rn_project_root+'/bundle/ios/ios-HMDS-'+version_new+'/index.ios.bundle.meta', target_dir_ios+'/index.ios.bundle.meta')


# 切换回根目录
os.chdir('../reactnative/HMDS/')


# ********** 生成素材差异相关文件代码 ************
print "\n======================================================"
print "================= 生成素材差异文件 =================\n"

print "素材补丁区间：", version_last, "-", version_new, "\n"

path_last = os.getcwd()+'/bundle/ios/ios-HMDS-'+version_last
path_new = os.getcwd()+'/bundle/ios/ios-HMDS-'+version_new

dir_patch = target_relative_dir_ios #素材补丁目标文件夹名称

if not os.path.exists(dir_patch):
    os.makedirs(dir_patch)

remove_txt_name = dir_patch+"/remove_assets_patch.txt"
fp = open(remove_txt_name, 'w')

def copy_add_files(dcmp): #拷贝增加的素材文件
    for sub_dcmp in dcmp.subdirs.values():
        copy_add_files(sub_dcmp)
    for o_r in dcmp.right_only:
        if ('.png' in o_r) | ('.jpg' in o_r):
            # 增加的素材文件 完整路径
            full_file_path = "%s/%s" % (dcmp.right, o_r)
            print ("add files: %s\ndir: %s\n---------" % (o_r, full_file_path.replace(path_new, '')))
            # 目标文件的文件夹 完整路径
            target_file_dir = os.getcwd()+'/'+dir_patch+full_file_path.replace(path_new, '').replace(o_r, '')
            if not os.path.exists(target_file_dir):
                os.makedirs(target_file_dir)
            shutil.copyfile(full_file_path, target_file_dir+o_r)

def record_remove_files(dcmp): #记录移除的素材 相对路径
    for sub_dcmp in dcmp.subdirs.values():
        record_remove_files(sub_dcmp)
    for o_l in dcmp.left_only:
        if ('.png' in o_l) | ('.jpg' in o_l):
            full_dir = "%s/%s" % (dcmp.left, o_l)
            relative_dir = full_dir.replace(path_last, '')
            print("remove files: %s\ndir: %s\n---------" % (o_l, relative_dir))
            fp.write(relative_dir+"\n")

print "============ 增加的素材文件 ============"
dcmp_add = dircmp(path_last, path_new)
copy_add_files(dcmp_add)

print "\n============ 移除的素材文件 ============"
dcmp_remove = dircmp(path_last, path_new)
record_remove_files(dcmp_remove)

fp.close()
